// @file      SessionManager.groovy
// @author    Mac Radigan
// @purpose   YCOA web interface session management

package ycoa.session

import ycoa.service.*
import ycoa.markup.*
import ycoa.configuration.*

def class SessionManager {

  private ServiceClient service = null
  private ConfigObject configuration = new ConfigObject()
  private Renderer renderer = new Renderer("")
  private String host = null
  private String market = null
  private int port = 0
  private String username = null
  private String password = null
  private String root = System.getenv("YCOA_WEB_HOME")

  public SessionManager() {
    setConfiguration()
    setMarket("test")
  }

  public SessionManager(market) {
    setConfiguration()
    setMarket(market)
  }

  private setConfiguration() {
    configuration = YCOAConfiguration.getConfiguration()
  }

  public String getMarket() {
    return this.market
  }

  public ConfigObject getConfiguration() {
    return this.configuration
  }

  public setMarket(market) {
    this.market = market
    switch( market.toLowerCase() ) {
      case ["az","arizona"]:
        this.host = configuration.server.az.host
        this.port = configuration.server.az.port
        break
      case ["test"]:
        this.host = configuration.server.test.host
        this.port = configuration.server.test.port
        break
      case ["kc","ks","kansas"]:
        this.host = configuration.server.ks.host
        this.port = configuration.server.ks.port
        break
      default:
        throw new Exception("host not specified.")
        break
    }
  }

  public String getHost() {
    return this.host
  }

  public int getPort() {
    return this.port
  }

  public String setCredentials(String username, String password) {
    this.username = username
    this.password = password
    this.service = new ServiceClient(this.host, this.port, username, password)
  }

  public ServiceClient getServiceClient() {
    return service
  }

  public Renderer getRenderer() {
    return renderer
  }

} // end of class SessionManager
